//
//  LGTTabBarVC.swift
//  KJLGT
//
//  Created by jenkin on 2019/8/7.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit
import SnapKit
import Alamofire
//import Masonry

class LGTTabBarVC: UITabBarController, UITabBarControllerDelegate {
    var currentIndex:Int = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setUI()
        //self.setBlock()
        self.delegate = self
//        //用户角色改变
//        NotificationCenter.default.addObserver(self, selector: #selector(userTypeChangeAction), name: User_Role_Change_Note, object: nil)
        //NotificationCenter.default.addObserver(self, selector: #selector(userTypeChangeAction), name: User_Role_Change_Note, object: nil)
        
        //退出登录
        NotificationCenter.default.addObserver(self, selector: #selector(quitAction), name: Offline_Note, object: nil)
        //消息数量处理
        NotificationCenter.default.addObserver(self, selector: #selector(messageAction), name: App_Bage_Note, object: nil)
    }
    
    @objc func userTypeChangeAction(){
        self.setInitVC()
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self, name: Offline_Note, object: nil)
        NotificationCenter.default.removeObserver(self, name: App_Bage_Note, object: nil)
        //NotificationCenter.default.removeObserver(self, name: User_Role_Change_Note, object: nil)
    }
    
    //退出登录
    @objc func quitAction(note:Notification){
        if UserModel.share != nil{
            weak var weakSelf = self
            let alertVC = UIAlertController.init(title: "您的登录信息已过期，请重新登录", message: "", preferredStyle: UIAlertController.Style.alert)
            let playAction = UIAlertAction.init(title: "确定", style: UIAlertAction.Style.default) { (action) in
                weakSelf?.logout(login:true)
            }
            let cancelAction = UIAlertAction.init(title: "取消", style: UIAlertAction.Style.default) { (action) in
                weakSelf?.logout()
            }
            alertVC.addAction(playAction)
            alertVC.addAction(cancelAction)
            self.present(alertVC, animated: true, completion: nil)
        }
    }
    
    func logout(login:Bool = false){
        self.selectedIndex = 0
        if login == true{
            let mainVC:UINavigationController =  self.viewControllers?[0] as! UINavigationController
            mainVC.pushViewController(LGTMainLoginVC(), animated: true)
            self.navigationController?.popViewController(animated: false)
        }
        //清理数据
        LGTUserUtil.share.clearUserInfo()
        UserModel.share = nil
    }
    
    func setUI(){
        self.tabBar.tintColor = TabBarItemSelectColor
        self.setInitVC()
    }
    
    func setInitVC(){
        //流量主
        if UserModel.share == nil || UserModel.share?.user_type == 2{
            let homeVC:LGTHomeVC = LGTHomeVC()
            self.addChildVC(vc: homeVC, title: "主页", imageName: "homepage", selectedImageName: "homepage_icon")
            let taskVC:LGTTaskVC = LGTTaskVC()
            self.addChildVC(vc: taskVC, title: "任务", imageName: "task", selectedImageName: "taskselection")
            let orderVC:LGTOrderVC = LGTOrderVC()
            self.addChildVC(vc: orderVC, title: "订单", imageName: "order", selectedImageName: "order_selection")
            let personVC:LGTPersonVC = LGTPersonVC()
            self.addChildVC(vc: personVC, title: "我的", imageName: "mine", selectedImageName: "mine_selection")
            return
        }
        
        //任务主
        let taskVC:LGTRWZMainTaskVC = LGTRWZMainTaskVC()
        self.addChildVC(vc: taskVC, title: "任务", imageName: "task", selectedImageName: "taskselection")
        let orderVC:LGTRWZOrderVC = LGTRWZOrderVC()
        self.addChildVC(vc: orderVC, title: "订单", imageName: "order", selectedImageName: "order_selection")
        let publicVC:LGTBaseVC = LGTBaseVC()
        self.addChildVC(vc: publicVC, title: "发布", imageName: "release", selectedImageName: "release_selection")
        let personVC:LGTPersonVC = LGTPersonVC()
        self.addChildVC(vc: personVC, title: "我的", imageName: "mine", selectedImageName: "mine_selection")
    }
    
    func addChildVC(vc:UIViewController, title:String, imageName:String, selectedImageName:String)->Void{
        let nav:LGTNavgationController = LGTNavgationController.init(rootViewController: vc)
        let image:UIImage = UIImage.init(named: imageName) ?? UIImage.init()
        let selectImage:UIImage = UIImage.init(named: selectedImageName) ?? UIImage.init()
        let tabBarItem:UITabBarItem = UITabBarItem.init(title: title, image: image, selectedImage: selectImage)
        tabBarItem.badgeColor = RedColor
        nav.tabBarItem = tabBarItem;
        self.addChild(nav)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //系统消息数量 显示处理
        self.messageAction(note: Notification.init(name: App_Bage_Note))
        
        //推送消息未读
        if LGTPushUtil.share.pushInfo != nil{
            let appdelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
            LGTPushUtil.share.pushAction(appdelegate: appdelegate, pushInfo: LGTPushUtil.share.pushInfo!)
        }
        
        if UserModel.share == nil{
            return
        }
        
        //user_type  0 没有选择角色  1-任务发布者 2-任务接收者]
        if UserModel.share!.user_type == 0{
            let nav:UINavigationController = self.viewControllers![self.selectedIndex] as! UINavigationController
            //角色选择
            let choseVC:LGTUserChoseVC = LGTUserChoseVC()
            choseVC.fromTabbarState = true
            nav.pushViewController(choseVC, animated: true)
            return
        }
    }
    
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController){
        //推送的待处理
        
        //流量主
        if UserModel.share == nil || UserModel.share?.user_type == 2{
            if UserModel.share == nil{
                if self.selectedIndex == 2 || self.selectedIndex == 3{
                    self.selectedIndex = self.currentIndex
                    self.getLoginVC()
                }
            }
        }
        //任务主
        else{
            if UserModel.share != nil{
                if self.selectedIndex == 2{
                    self.selectedIndex = self.currentIndex
                    let nav:UINavigationController = self.viewControllers?[self.currentIndex] as! UINavigationController
                    nav.pushViewController(LGTRWZPostVC(), animated: true)
                }
                self.currentIndex = self.selectedIndex
                return
            }

            if self.selectedIndex == 1 || self.selectedIndex == 2 || self.selectedIndex == 3{
                self.selectedIndex = self.currentIndex
                self.getLoginVC()
            }
        }
        self.currentIndex = self.selectedIndex
    }
    
    func getLoginVC(){
        let nav:UINavigationController = self.viewControllers?[self.currentIndex] as! UINavigationController
        nav.pushViewController(LGTMainLoginVC(), animated: true)
    }
    
    @objc func messageAction(note:Notification){
        let user:UserModel? = UserModel.share
        if user == nil{
            return
        }
        
        let model:AppBadgeModel = AppModel.share?.appBadge ?? AppBadgeModel()
        //1-任务发布者
        if user?.user_type == 1{
            //任务
            let taskItem = self.tabBar.items?[0]
            if model.tp_task_count ?? 0 > 0{
                taskItem?.badgeValue = String(describing: model.tp_task_count!)
                if model.tp_task_count ?? 0 > 99{
                    taskItem?.badgeValue = "99+"
                }
            }else{
                taskItem?.badgeValue = nil
            }

            //订单
            let orderItem = self.tabBar.items?[1]
            if model.tp_task_order_count ?? 0 > 0{
                orderItem?.badgeValue = String(describing: model.tp_task_order_count!)
                if model.tp_task_order_count ?? 0 > 99{
                    orderItem?.badgeValue = "99+"
                }
            }else{
                orderItem?.badgeValue = nil
            }
            return
        }
        
        //2-任务接收者
        //流量主订单
        let orderItem = self.tabBar.items?[2]
        if model.tr_task_order_count ?? 0 > 0{
            orderItem?.badgeValue = String(describing: model.tr_task_order_count!)
            if model.tr_task_order_count ?? 0 > 99{
                orderItem?.badgeValue = "99+"
            }
        }else{
            orderItem?.badgeValue = nil
        }
    }
}
